package com.cwx.mychat.controller;

import com.cwx.mychat.entity.constant.CodeConstant;
import com.cwx.mychat.entity.dto.TokenUserInfoDto;
import com.cwx.mychat.entity.enums.ExceptionBizCodeEnum;
import com.cwx.mychat.entity.po.GroupInfo;
import com.cwx.mychat.exception.BusinessException;
import com.cwx.mychat.service.GroupInfoService;
import com.cwx.mychat.utils.R;
import com.cwx.mychat.utils.common.CommonUtil;
import com.cwx.mychat.utils.threadLocal.ThreadLocalUtil;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/group")
public class GroupInfoController extends BaseController<GroupInfoService, GroupInfo>{


    @Autowired
    private GroupInfoService groupInfoService;

    /**
     * 新建群组
     */
    @PostMapping("/saveGroup")
    public R saveGroup(@RequestParam("groupAvtar")MultipartFile file, @RequestParam("groupName") String groupName,
                        @RequestParam("groupTip") String groupTip, @RequestParam("joinType") String joinType) throws BusinessException, IOException {
        //获取用户信息
        TokenUserInfoDto tokenUserInfoDto = CommonUtil.getSecurityContext();
        GroupInfo groupInfo = new GroupInfo();
        groupInfo.setGroupOwnerId(tokenUserInfoDto.getUserId());
        groupInfo.setGroupName(groupName);
        groupInfo.setJoinType(Integer.parseInt(joinType));
        groupInfo.setGroupTip(groupTip);
        return this.groupInfoService.saveGroup(groupInfo, file);

    }

    /**
     * 删除群组
     * @param groupId
     * @return
     */
    @DeleteMapping("/delGroup/{groupId}")
    public R deleteGroup(@PathVariable("groupId") String groupId) throws BusinessException {
        return groupInfoService.deleteGroup(groupId);
    }

    /**
     * 修改群组信息
     */
    @PutMapping("/editGroup")
    public R editGroup(@NotEmpty String groupName,
                       @NotNull Integer joinType,
                       @NotEmpty String groupId) throws BusinessException {
        //获取用户信息
        TokenUserInfoDto tokenUserInfoDto = CommonUtil.getSecurityContext();
        GroupInfo groupInfo = new GroupInfo();
        groupInfo.setGroupOwnerId(tokenUserInfoDto.getUserId());
        groupInfo.setGroupName(groupName);
        groupInfo.setJoinType(joinType);
        groupInfo.setGroupId(groupId);
        this.groupInfoService.updateGroup(groupInfo);
        return R.ok();
    }

    /**
     * 获取所有群组和我的好友
     * @return
     */
    @GetMapping("getAll")
    public R getAll(){
        //获取用户信息
        TokenUserInfoDto tokenUserInfoDto = CommonUtil.getSecurityContext();
        R r = groupInfoService.getUserAndGroupList(tokenUserInfoDto);
        return R.ok().put(CodeConstant.RESULT_MSG, r);
    }

    /**
     * 获取群组详情
     * @return
     */
    @GetMapping("/getInfo/{groupId}")
    public R getInfo(@PathVariable String groupId) throws BusinessException {
        return groupInfoService.getInfoById(groupId);
    }

    /**
     * 获取群组的所有详情信息，包括备注，昵称，人员
     * @param groupId
     * @param userId
     * @return
     * @throws BusinessException
     */
    @GetMapping("/getGroupAllInfoById")
    public R getGroupAllInfoById(@RequestParam("groupId") String groupId, @RequestParam("userId") String userId) throws BusinessException {
        if(!userId.equals(CommonUtil.getSecurityContext().getUserId())){
            throw new BusinessException(ExceptionBizCodeEnum.NOT_FOUND_ACCOUNT);
        }
        return groupInfoService.getGroupAllInfoById(groupId, userId);
    }



}
